package com.smarthealthring.mm.widgets;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;

/**
 * Created by sunxl8 on 2017/12/7.
 */

public class CheckValidEditText extends AppCompatEditText implements CheckStatusTextView.ValidStatusObserable {

    public CheckValidEditText(Context context) {
        super(context);
        init();
    }

    public CheckValidEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mListener != null) {
                    mListener.status(isValid());
                }
            }
        });
    }

    private CheckStatusTextView.StatusListener mListener;

    @Override
    public void setListener(CheckStatusTextView.StatusListener listener) {
        mListener = listener;
    }

    @Override
    public boolean isValid() {
        return TextUtils.isEmpty(getText().toString().trim()) ? false : true;
    }
}
